#!/usr/bin/env python
# Create: 2020/1/16
__author__ = '749B'

import queue
import socketserver
import threading

from core import output
from conf import settings


syslog_query = queue.Queue()


class UDPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        data, _ = self.request
        syslog_query.put(data.decode())


def run():
    # 守护线程：当主线程退出时，守护线程就会退出
    # 用于从队列中去数据然后输出
    task = threading.Thread(target=output.run, args=(syslog_query,))
    task.setDaemon(True)
    task.start()
    # 主线程，收集syslog并存入队列
    server = socketserver.UDPServer((settings.HOST, settings.PORT), UDPHandler)
    try:
        server.serve_forever()
    except KeyboardInterrupt as e:
        pass
    finally:
        print("运行结束")



